package com.example.tilitili.controller;


import com.example.tilitili.pojo.Goods;
import com.example.tilitili.service.IGoodsService;
import com.example.tilitili.service.ISeckillGoodsService;
import com.example.tilitili.vo.GoodsVo;
import com.example.tilitili.vo.RespBean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.WebRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

@Controller
@RequestMapping("/seckillground")
@Slf4j
public class SeckillgroundController {
    @Autowired
    private ISeckillGoodsService seckillGoodsService;
    @Autowired
    private IGoodsService goodsService;

    /*
     *跳转秒杀上架界面
     */
    @RequestMapping("/toseckillground/{goodsId}")
    public String toseckillground(Model model,@PathVariable Long goodsId){
        Goods goods=goodsService.findGoodsVoByGoodsId2(goodsId);
//        log.info("{}",goods.getId());
        model.addAttribute("seckillground",goods);
        return "seckillground";
    }

    /*
     *秒杀上架功能
     */
    @RequestMapping("/doseckillground")
    @ResponseBody
    public RespBean doseckillground(@Valid GoodsVo goodsVo,HttpServletRequest request, HttpServletResponse response){
//        log.info("{}",goodsVo);
//        return null;
        return seckillGoodsService.doseckillground(goodsVo,request,response);
    }
/*
    @InitBinder
    public void initBinder(WebDataBinder binder, WebRequest request) {
        //转换日期 注意这里的转化要和传进来的字符串的格式一直 如2015-9-9 就应该为yyyy-MM-dd
        DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));// CustomDateEditor为自定义日期编辑器
    }*/
}
